package com.qf.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.enums.ResultEnum;
import com.qf.exeception.EntryExeception;
import com.qf.mapper.CategoryMapper;
import com.qf.mapper.EntryMapper;
import com.qf.pojo.EbookCategory;
import com.qf.pojo.EbookEntry;
import com.qf.service.EntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EntryServiceImpl implements EntryService {
   @Autowired
   EntryMapper entryMapper;
   @Autowired
   CategoryMapper categoryMapper;

    @Override
    public PageInfo<EbookEntry> findByPage(Integer page, Integer size, String name) {


        PageHelper.startPage(page, size);
        List<EbookEntry> entrylist = entryMapper.entrylist(name);

        PageInfo<EbookEntry> pageInfo=new PageInfo<>(entrylist);

        return pageInfo;

    }

    @Override
    public List<EbookCategory> findAllCategory() {
        List<EbookCategory> categoryList = categoryMapper.selectAll();
        return categoryList;
    }

    @Override
    public Integer saveData(EbookEntry ebookEntry) {
        ebookEntry.setCategoryid(1);
        int count = entryMapper.insertSelective(ebookEntry);
        return count;
    }




    @Override
    public EbookEntry findById(Integer id) {
        return entryMapper.selectByPrimaryKey(id);
    }



    @Override
    public Integer updateData(EbookEntry ebookEntry) {
        ebookEntry.setCategoryid(2);
        int updatecount = entryMapper.updateByPrimaryKeySelective(ebookEntry);

        if (updatecount!=1){
            throw new EntryExeception(ResultEnum.UPDATE_ERROR);
        }
        return updatecount;
    }

    @Override
    public Integer deleteById(Integer id) {
        return entryMapper.deleteByPrimaryKey(id);
    }

//    以下为测试缓存方法与项目无关
    public void queryName(){
        List<EbookEntry> ebookEntries = entryMapper.selectAll();
        for (EbookEntry ebookEntry : ebookEntries) {
            System.out.println(ebookEntry);
        }
    }

    @Override
    public EbookEntry queryByName(Integer id) {

        return entryMapper.selectByPrimaryKey(1);
    }
}



